package com.wcpdoc.exam.core.runner;

import com.wcpdoc.exam.core.entity.MyExamMock;
import com.wcpdoc.exam.core.service.MyExamMockService;
import com.wcpdoc.exam.core.service.MyExamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * 考试核心启动
 * 
 * v1.0 zhanghc 2019年9月29日下午2:32:16
 */
@Component
public class ExamMockEndRunner implements ApplicationRunner {
	private static final Logger log = LoggerFactory.getLogger(ExamMockEndRunner.class);
	
	@Resource
	private MyExamMockService myExamMockService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		// SpringApplication.callRunners方法会顺序执行ApplicationRunner实现，while (true)不返回导致其他任务不执行
		new Thread(new Runnable() {
			@Override
			public void run() {
				// 监听阅卷时间到，自动阅卷
				while (true) {
					try {
						TimeUnit.SECONDS.sleep(10);
					} catch (InterruptedException e1) {
						log.error("自动阅卷(已经考试超时的模拟考试)错误：", e1);
					}
					try{
						myExamMockService.endOverExam();
					}catch (Exception e) {
						log.error("自动阅卷(已经考试超时的模拟考试)错误：", e);
					}
				}
			}
		}).start();
	}
}

